package practice_list;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-03-21
 * Time:13:09
 */
public class 两两交换链表中的节点 {

    /*
    * 思路： 我们定义三个结点，只要明确这三个结点，很好交换。
    * */

    public ListNode swapPairs(ListNode head) {
        ListNode nood = new ListNode(0);
        nood.next = head;
        ListNode prev = nood;

        while (prev.next != null && prev.next.next != null) {
            ListNode cur = prev.next;
            ListNode curNext = prev.next.next;
            prev.next = curNext;
            cur.next = curNext.next;
            curNext.next = cur;
            prev = cur;
        }
        return nood.next;
    }

}
